//
//  AMPLoginView.swift
//  LivingHelper
//
//  Created by Ampaw on 2017/9/13.
//  Copyright © 2017年 Ampaw. All rights reserved.
//

import UIKit

// imgView 的宽高
private let kImgViewWidth_Height: CGFloat = 100
// 顶部距离账号输入框的高度
private let kTopSpacePassportHeight: CGFloat = 80

// 定义协议
protocol AMPLoginViewDelegate: NSObjectProtocol {
    func didLoginClick()            // 登陆
    func didRegisterClick()         // 注册
    func didForgetPwdCilck()        // 忘记密码
    func didOtherQQLoginClick()     // 三方QQ登陆
    func didOtherWeiXinLoginCilck() // 三方微信登陆
    func didOtherWeiBoLoginClick()  // 三方微博
}

class AMPLoginView: UIView {
    
    // MARK: - 属性
    weak var loginViewDelegate: AMPLoginViewDelegate?
    
    // MARK: - 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 准备UI
    private func setUpUI() {
        addSubViews()
        addConstraints()
    }
    
    // MARK: - Event
    @objc private func loginAction() {
        loginViewDelegate?.didLoginClick()
    }
    
    @objc private func registerAction() {
        loginViewDelegate?.didRegisterClick()
    }
    
    @objc private func forgetPwdAction() {
        loginViewDelegate?.didForgetPwdCilck()
    }
    
    @objc private func otherQQLoginAction() {
        loginViewDelegate?.didOtherQQLoginClick()
    }
    
    @objc private func otherWeiXinLoginAction() {
        loginViewDelegate?.didOtherWeiXinLoginCilck()
    }
    
    @objc private func otherWeiBoLoginAction() {
        loginViewDelegate?.didOtherWeiBoLoginClick()
    }
    
    // MARK: - 私有方法
    /// 添加控件
    private func addSubViews() {
        self.addSubview(self.aboutImgView)
        self.addSubview(self.passportTextField)
        self.addSubview(self.passwordTextField)
        self.addSubview(self.loginButton)
        self.addSubview(self.registerButton)
        self.addSubview(self.forgetButton)
        self.addSubview(self.otherLoginLabel)
        self.addSubview(self.leftLine)
        self.addSubview(self.rightLine)
        self.addSubview(self.otherWeiXinLoginButton)
        self.addSubview(self.otherQQLoginButton)
        self.addSubview(self.otherWeiBoLoginButton)
        
        self.loginButton.addTarget(self, action: #selector(loginAction), for: UIControlEvents.touchUpInside)
        self.registerButton.addTarget(self, action: #selector(registerAction), for: UIControlEvents.touchUpInside)
        self.forgetButton.addTarget(self, action: #selector(forgetPwdAction), for: UIControlEvents.touchUpInside)
        self.otherQQLoginButton.addTarget(self, action: #selector(otherQQLoginAction), for: UIControlEvents.touchUpInside)
        self.otherWeiXinLoginButton.addTarget(self, action: #selector(otherWeiXinLoginAction), for: UIControlEvents.touchUpInside)
        self.otherWeiBoLoginButton.addTarget(self, action: #selector(otherWeiBoLoginAction), for: UIControlEvents.touchUpInside)
    }
    ///添加约束
    private func addConstraints() {
        unowned let WS = self
        
        self.aboutImgView.snp.makeConstraints { (make) -> Void in
            make.centerX.equalTo(WS).offset(0)
            make.top.equalTo(WS).offset(kTopSpacePassportHeight*0.5)
            make.width.height.equalTo(kImgViewWidth_Height)
        }
        
        self.passportTextField.snp.makeConstraints { (make) -> Void in
            make.centerX.equalTo(WS).offset(0)
            make.top.equalTo(WS.aboutImgView.snp.bottom).offset(kTopSpacePassportHeight*0.5)
            make.width.equalTo(AMPConstants.AMP_SCREEN_WIDTH*0.8)
            make.height.equalTo(44)
        }
        
        self.passwordTextField.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(WS.passportTextField.snp.bottom).offset(10)
            make.centerX.width.height.equalTo(WS.passportTextField)
        }
        
        self.loginButton.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(WS.passwordTextField.snp.bottom).offset(10)
            make.centerX.width.height.equalTo(WS.passportTextField)
        }
        
        self.registerButton.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(WS.loginButton.snp.bottom).offset(10)
            make.left.height.equalTo(WS.loginButton)
            make.width.equalTo(60)
        }
        
        self.forgetButton.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(WS.loginButton.snp.bottom).offset(10)
            make.right.height.equalTo(WS.loginButton)
            make.width.equalTo(100)
        }
        
        self.otherLoginLabel.snp.makeConstraints { (make) -> Void in
            make.centerX.equalTo(WS).offset(0)
            make.bottom.equalTo(WS.snp.bottom).offset(-100)
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        
        self.leftLine.snp.makeConstraints { (make) -> Void in
            make.left.equalTo(WS.loginButton.snp.left)
            make.centerY.equalTo(WS.otherLoginLabel.snp.centerY)
            make.right.equalTo(WS.otherLoginLabel.snp.left)
            make.height.equalTo(1)
        }
        
        self.rightLine.snp.makeConstraints { (make) -> Void in
            make.right.equalTo(WS.loginButton.snp.right)
            make.centerY.equalTo(WS.otherLoginLabel.snp.centerY)
            make.left.equalTo(WS.otherLoginLabel.snp.right)
            make.height.equalTo(1)
        }
        
        self.otherWeiXinLoginButton.snp.makeConstraints { (make) -> Void in
            make.centerX.equalTo(WS).offset(0)
            make.top.equalTo(WS.otherLoginLabel.snp.bottom).offset(10)
            make.width.height.equalTo(40)
        }
        
        self.otherQQLoginButton.snp.makeConstraints { (make) -> Void in
            make.centerX.equalTo(WS).offset(-70)
            make.centerY.width.height.equalTo(WS.otherWeiXinLoginButton)
        }
        
        self.otherWeiBoLoginButton.snp.makeConstraints { (make) -> Void in
            make.centerX.equalTo(WS).offset(70)
            make.centerY.width.height.equalTo(WS.otherWeiXinLoginButton)
        }
    }
    
    // MARK: - 懒加载
    lazy var passportTextField: UITextField = {
        let passport = UITextField()
        passport.backgroundColor = UIColor.white
        passport.layer.cornerRadius = 5.0;
        passport.layer.masksToBounds = true
        passport.layer.borderColor = UIColor.white.cgColor
        passport.layer.borderWidth = 1.0
        passport.font = AMPConstants.AMP_Font14
        passport.placeholder = "请输入账号"
        passport.clearButtonMode = UITextFieldViewMode.whileEditing // 设置“清除”按钮
        passport.autocapitalizationType = UITextAutocapitalizationType.none  // 去除首字母自动大写
        
        let leftImg = UIImageView()
        leftImg.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        leftImg.image = UIImage(named: "login_icon_passport")
        passport.leftView = leftImg
        passport.leftViewMode = UITextFieldViewMode.always
        
        return passport
    }()
    
    lazy var passwordTextField: UITextField = {
        let password = UITextField()
        password.backgroundColor = UIColor.white
        password.layer.cornerRadius = 5.0;
        password.layer.masksToBounds = true
        password.layer.borderColor = UIColor.white.cgColor
        password.layer.borderWidth = 1.0
        password.font = AMPConstants.AMP_Font14
        password.placeholder = "请输入密码"
        password.clearButtonMode = UITextFieldViewMode.whileEditing
        password.autocapitalizationType = UITextAutocapitalizationType.none
        password.isSecureTextEntry = true // 设置密码，暗文显示
        
        let leftImg = UIImageView()
        leftImg.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        leftImg.image = UIImage(named: "login_icon_password")
        password.leftView = leftImg
        password.leftViewMode = UITextFieldViewMode.always
        
        return password
    }()
    
    private lazy var aboutImgView: UIImageView = {
        let imageView = UIImageView()
        imageView.backgroundColor = UIColor.clear
        imageView.image = UIImage(named: "about_icon")
        imageView.sizeToFit()
        return imageView
    }()
    
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        button.backgroundColor = AMPConstants.AMPMainColor
        button.layer.cornerRadius = 5.0;
        button.layer.masksToBounds = true
        button.titleLabel?.font = AMPConstants.AMP_Font18
        button.setTitle("登录", for: UIControlState.normal)
        button.setTitleColor(UIColor.white, for: UIControlState.normal)
        button.setTitleColor(UIColor.black, for: UIControlState.highlighted)
        
        return button
    }()
    
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        button.backgroundColor = UIColor.clear
        button.titleLabel?.font = AMPConstants.AMP_Font16
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left
        button.setTitle("注册", for: UIControlState.normal)
        button.setTitleColor(AMPConstants.AMPMainColor, for: UIControlState.normal)
        button.setTitleColor(UIColor.black, for: UIControlState.highlighted)
        
        return button
    }()
    
    private lazy var forgetButton: UIButton = {
        let button = UIButton()
        button.backgroundColor = UIColor.clear
        button.titleLabel?.font = AMPConstants.AMP_Font16
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignment.right
        button.setTitle("忘记密码", for: UIControlState.normal)
        button.setTitleColor(AMPConstants.AMPMainColor, for: UIControlState.normal)
        button.setTitleColor(UIColor.black, for: UIControlState.highlighted)
        
        return button
    }()
    
    private lazy var leftLine: UIView = {
        let leftLine = UIView()
        leftLine.backgroundColor = AMPConstants.AMPMainColor
        
        return leftLine
    }()
    
    private lazy var otherLoginLabel: UILabel = {
        let otherLabel = UILabel()
        otherLabel.backgroundColor = UIColor.clear
        otherLabel.textAlignment = NSTextAlignment.center
        otherLabel.textColor = AMPConstants.AMPMainColor
        otherLabel.font = AMPConstants.AMP_Font14
        otherLabel.text = "其他方式登录"
        
        return otherLabel
    }()
    
    private lazy var rightLine: UIView = {
        let rightLine = UIView()
        rightLine.backgroundColor = AMPConstants.AMPMainColor
        
        return rightLine
    }()
    
    private lazy var otherQQLoginButton: UIButton = {
        let button = UIButton()
        button.backgroundColor = UIColor.clear
        button.setImage(UIImage(named: "login_icon_qq"), for: UIControlState.normal)
        
        return button
    }()
    
    private lazy var otherWeiXinLoginButton: UIButton = {
        let button = UIButton()
        button.backgroundColor = UIColor.clear
        button.setImage(UIImage(named: "login_icon_wechat"), for: UIControlState.normal)
        
        return button
    }()
    
    private lazy var otherWeiBoLoginButton: UIButton = {
        let button = UIButton()
        button.backgroundColor = UIColor.clear
        button.setImage(UIImage(named: "login_icon_weibo"), for: UIControlState.normal)
        
        return button
    }()
}
